<script lang="ts">
import type {PropType} from "vue";
import {use_this_admin} from "@/store/this_admin.ts";
import {use_this_tenant} from "@/store/config_map.ts";
// @ts-ignore
import {param_encode, snake_str} from "@/js/s94_tool";
// @ts-ignore
import {iframe} from "@/js/tool";

interface TypeAction {
  ca: string; // 操作对应权限判定，支持空权限(不限制)
  type?: 'add'|'edit'|'list'|'info'|'del'|'recycle'|'restore'|'delete'; // 操作类型，默认：根据ca推定
  title?: string; // 操作标题，默认：根据ca推定
  button_type?: 'default' | 'primary' | 'success' | 'warning' | 'danger' | 'info' | '' | 'text'; // 按钮类型 默认：根据ca推定
  round?: boolean; // 圆角按钮 默认：根据ca推定
  unique?: string | number, // 操作唯一码，如果不为空，那么已存在相同码操作的情况下，会忽略
  action_type?: string; // 操作类型，默认：使用 iframe 打开对应操作的页面窗口，传入项目ID。其他情况请在 action 事件内处理
}
export default {
  _title_: '操作列表',
  _tip_: '根据传入的项目数据，自动匹配可用的操作按钮列表',
  _css_var_: {},
  components: {},
  props: {
    table_name: {
      type: String,
      required: true,
      _label_: '控制器',
      _tip_: '是否启用下拉刷新',
    },
    info: {
      type: Object,
      _label_: '项目数据',
      _tip_: '',
    },
    list: {
      type: Array as PropType<TypeAction[]>,
      _label_: '操作列表',
      _tip_: '',
    },
    ca_list: {
      type: Array as PropType<string[]>,
      _label_: '操作列表-简易',
      _tip_: '每一项只传入权限的操作列表，不支持空权限，其他如title、is_edit参数根据权限推定，一项中多个权限用|分隔，用于表示多个操作唯一生效',
    }
  },
  emits: ['action'],
  data(){
    return {}
  },
  methods: {
    auto_data(action_row:TypeAction){
      const title_mpa = {
        add: '添加',
        edit: '编辑',
        list: '列表',
        info: '详情',
        del: '删除',
        recycle: '回收站',
        restore: '恢复',
        delete: '彻底删除',
      }
      if (typeof action_row.type === 'undefined'){
        let a = action_row.ca.split('/').pop();
        // @ts-ignore
        action_row.type = title_mpa[a] ? a : 'edit';
      }
      if (typeof action_row.title === 'undefined'){
        // @ts-ignore
        action_row.title = title_mpa[action_row.type] || '-';
      }
      if (typeof action_row.button_type === 'undefined'){
        const button_type_map = {
          primary: ['edit', 'add'],
          danger: ['del', 'delete']
        }
        action_row.button_type = '';
        Object.keys(button_type_map).some(button_type=>{
          // @ts-ignore
          if (~button_type_map[button_type].indexOf(action_row.type)) {
            // @ts-ignore
            action_row.button_type = button_type;
            return true;
          }
        })
      }
      return action_row;
    },
    on_click(action_row:TypeAction){
      if (!action_row.action_type) {
        let c = snake_str(action_row.ca.split('/').shift());

        let type = action_row.type || '';
        if (~['info','edit','add'].indexOf(type)){
          let path = `${c}/edit`
          let param = []
          if (this.info?.id) param.push(`id=${this.info.id}`)
          if (action_row.type == 'info') param.push('is_info=1')
          if (param.length) path += ('?'+param.join('&'))
          iframe({href:path})
        }else if(~['list','recycle'].indexOf(type)) {
          let path = `${c}/index`
          iframe({href:path})
        }else if(this.info?.id && ~['del','restore','delete'].indexOf(type)){
          let url = `/admin/${c}/${type}?ids=${this.info.id}`;
          // TODO 确认操作

        }
      }
    }
  },
  computed: {
    action_list(){
      let res = this.list ? [...this.list] : [];
      let auto_unique = Date.now();
      (this.ca_list || []).forEach(ca_list_row=>{
        if (!ca_list_row) return;
        let arr = ca_list_row.split('|');
        let unique = arr.length > 1 ? auto_unique++ : undefined;
        arr.forEach(ca=>{
          res.push({ca, unique})
        })
      })
      let unique_list:any[] = [];
      const this_tenant_id = use_this_tenant().id;
      res = res.map(action_row=>{
        // 对不存在的参数进行推定
        return this.auto_data(action_row);
      }).filter(action_row=>{
        // 唯一码筛选
        if (action_row.unique){
          if (~unique_list.indexOf(action_row.unique)) return false;
          unique_list.push(action_row.unique)
        }
        // 权限筛选
        if (!use_this_admin().in_right(action_row.ca)) return false;
        // 项目租户ID筛选
        // @ts-ignore
        if (this.info && !~['info','list'].indexOf(action_row.type)){
          if (this_tenant_id !== 0 && this.info.tenant_id !== this_tenant_id) return false;
        }
        return true;
      })
      return res;
    }
  }
}
</script>

<template>
  <div class="action-list">
    <el-button v-for="action_row in action_list" @click="$emit('action', action_row)" size="small" :type="action_row.button_type" :round="action_row.round">{{action_row.title}}</el-button>
  </div>
</template>

<style scoped>

</style>

https://appgateway.ifekh.com/gateway_web/app/mobile-app-composition/map/google/place-autocomplete
https://appgateway.ifekh.com/gateway_web/app/mobile-app-composition/map/google/place-autocomplete

components: "country:KH"input:"我”
language: "zh-CN"loginName:"855066688963”operatorNo:"1961078113462890496^sessionToken: "mfnr17t9j6lyw"

components:"country :KH"input:"的”
ge: "zh-CN"languagloginName:"855066688963"operatorNo: "1961078113462890496